package cn.net.withub.dataCollector.web.collector;

import cn.net.withub.dataCollector.common.utils.CreateNewKey;
import cn.net.withub.dataCollector.fastdfs.utils.FastDfsUtil;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfReaderContentParser;
import com.itextpdf.text.pdf.parser.SimpleTextExtractionStrategy;
import com.itextpdf.text.pdf.parser.TextExtractionStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.util.TextUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

/**
 * Created by yuanjie on 2018/5/3.
 */
public class HttpClientUtils {
    private  final String root = "/" + getClass().getResource("/").getFile().toString().replaceFirst("/", "");

    public static String getHtmlString(String targetUrl) {
        String content = null;

        HttpURLConnection connection = null;
        try {
            URL url = new URL(targetUrl);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)");
            connection.setRequestProperty("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*");
            connection.setRequestProperty("Accept-Language", "zh-cn");
            connection.setRequestProperty("UA-CPU", "x86");
            //为什么没有deflate呢
            connection.setRequestProperty("Accept-Encoding", "gzip");
            connection.setRequestProperty("Content-type", "text/html");
            //keep-Alive，有什么用呢，你不是在访问网站，你是在采集。嘿嘿。减轻别人的压力，也是减轻自己。
            connection.setRequestProperty("Connection", "close");
            //不要用cache，用了也没有什么用，因为我们不会经常对一个链接频繁访问。（针对程序）
            connection.setUseCaches(false);
            connection.setConnectTimeout(6 * 1000);
            connection.setReadTimeout(6 * 1000);
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestProperty("Charset", "utf-8");

            connection.connect();

            if (200 == connection.getResponseCode()) {
                InputStream inputStream = null;
                if (!TextUtils.isEmpty(connection.getContentEncoding())) {
                    String encode = connection.getContentEncoding().toLowerCase();
                    if (!TextUtils.isEmpty(encode) && encode.indexOf("gzip") >= 0) {
                        inputStream = new GZIPInputStream(connection.getInputStream());
                    }
                }

                if (null == inputStream) {
                    inputStream = connection.getInputStream();
                }

                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
                StringBuilder builder = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    builder.append(line).append("\n");
                }
                content = builder.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }

        return content;
    }

    public static String getHttpClient(String url) {
        String content = "";
        try{
            RequestConfig requestConfig = RequestConfig.custom()
                    .setConnectTimeout(5000)//一、连接超时：connectionTimeout-->指的是连接一个url的连接等待时间
                    .setSocketTimeout(5000)// 二、读取数据超时：SocketTimeout-->指的是连接上一个url，获取response的返回等待时间
                    .setConnectionRequestTimeout(5000)
                    .build();

            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);
            post.setConfig(requestConfig);
            HttpResponse response = client.execute(post);

            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                content = EntityUtils.toString(resEntity, "utf-8");
                System.out.println(content);
            } else {
                System.out.println("请求失败");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return content;
    }

    public static String s_of_Doc(InputStream fis){
        String str = "";
        try {
           //FileInputStream fis = new FileInputStream(new File("D:\\test\\test.doc"));
            HWPFDocument doc = new HWPFDocument(fis);
            str = doc.getDocumentText();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str;
    }

    public static String GetUrl(String url) {
        String index = "";
        HttpURLConnection httpURLConnection ;
        BufferedReader bf ;
        String readLine  ;
        try{
            URL u = new URL(url) ;
            httpURLConnection = (HttpURLConnection)u.openConnection() ;
            httpURLConnection.setRequestProperty("contentType", "application/msword");
            httpURLConnection.setConnectTimeout(5 * 1000);
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setRequestProperty("Charset", "GBK");
            int responsecode = httpURLConnection.getResponseCode() ;  // 返回码
            if(responsecode==200) {
                bf = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "GBK"));
                while ((readLine = bf.readLine()) != null) {
                    index += new String(readLine.getBytes(), "GBK");
                }
            }else{
                System.out.println("NOT  "+responsecode);
            }
        }catch(Exception e){
            System.out.println("Exception->"+e);
        }

        return index;
    }

    public static String getUrl(String u){
        StringBuffer sb = new StringBuffer();
        try {
            URL url = new URL(u);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestProperty("Charset", "GBK");
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            if (conn.getResponseCode() == 200) {
                InputStream is = conn.getInputStream();
                int len = 0;
                //原因就出在这里，直接我开的字节1024这回造成如果是一个中文字符正好在这个1024的临界点，这样就会出现中文乱码，所以我就直接将大小开到60000，哈哈哈。
                byte[] buf = new byte[60000];
                while ((len = is.read(buf)) != -1) {
                    sb.append(new String(buf, 0, len, "GBK"));
                }
                is.close();
            } else {
                throw new Exception("访问网络失败00");
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            try {
                throw new Exception("访问网络失败11");
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

        return sb.toString();
    }

    public static void saveToFile(String destUrl, String fileName)
            throws IOException {
        StringBuffer sb = new StringBuffer();
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        URL url = null;
        byte[] buf = new byte[1024];
        int size = 0;

        url = new URL(destUrl);
        httpUrl = (HttpURLConnection) url.openConnection();
        httpUrl.connect();

        bis = new BufferedInputStream(httpUrl.getInputStream());
        fos = new FileOutputStream(fileName);
        while ((size = bis.read(buf)) != -1){
            fos.write(buf, 0, size);
        }

        fos.close();
        bis.close();
        httpUrl.disconnect();
    }

    public static Map getWord(String destUrl) throws Exception {
        StringBuffer sb = new StringBuffer();
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        URL url = null;
        byte[] buf = new byte[1024];
        int size = 0;
        String filePath = "";
        String content = "";
        Map<String,Object> map = new HashMap<String, Object>();

        try{
            url = new URL(destUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.connect();

            InputStream is = httpUrl.getInputStream();
            //读取doc
//        HWPFDocument doc = new HWPFDocument(is);
//        Range rang = doc.getRange();
//        sb.append(rang.text());
//        sb.append(doc.getDocumentText());

            bis = new BufferedInputStream(is);

            // 如果不存在就创建文件夹
            //String desturl = root + CommonProperties.fzba_temtxt_save_url + "/" + fydm.toUpperCase() + "/";
            String directoryPath = "D:\\test\\";
            directoryPath = "/home/myapp/webCollector/temp";
            //root = "/" + getClass().getResource("/").getFile().toString().replaceFirst("/", "");
            if (!new File(directoryPath).exists()) {
                new File(directoryPath).mkdirs();
            }
            //写文件
            String fileName = new CreateNewKey().createId()+".doc";
            filePath = directoryPath+fileName;
            File file = new File(filePath);
            if(!file.exists())file.createNewFile();
            fos = new FileOutputStream(filePath);
            while ((size = bis.read(buf)) != -1){
                fos.write(buf, 0, size);
            }
            //关闭流
            fos.close();
            bis.close();
            is.close();
            httpUrl.disconnect();

            //读取doc
//        HWPFDocument doc = new HWPFDocument(new FileInputStream(new File(fileName)));
//        Range rang = doc.getRange();
//        sb.append(rang.text());
//        sb.append(doc.getDocumentText());

            WordExtractor wordExtractor = new WordExtractor(new FileInputStream(new File(filePath)));
            sb.append(wordExtractor.getText());

            //替换特殊字符
            content = sb.toString();
            content = content.replaceAll("\b","").replaceAll("\\a","");
            map.put("content",content);

            //上传至fastdfs服务器
            String serverFilePath = FastDfsUtil.Upload(filePath);
            map.put("serverFilePath",serverFilePath);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //删除文件
            if(!filePath.equals("")){
                File f = new File(filePath);
                if (f.exists()) {
                    f.delete();
                }
            }
        }


        return map;
    }

    public static Map getPdf(String destUrl) throws IOException {
        StringBuffer sb = new StringBuffer();
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        URL url = null;
        byte[] buf = new byte[1024];
        int size = 0;
        String filePath = "";
        String content = "";
        Map<String,Object> map = new HashMap<String, Object>();

        try{
            url = new URL(destUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.connect();

            InputStream is = httpUrl.getInputStream();
            //读取doc
//        HWPFDocument doc = new HWPFDocument(is);
////        Range rang = doc.getRange();
////        sb.append(rang.text());
//        sb.append(doc.getDocumentText());

            //写文件
            bis = new BufferedInputStream(is);

            // 如果不存在就创建文件夹
            String directoryPath = "D:\\test\\";
            directoryPath = "/home/myapp/webCollector/temp";
            if (!new File(directoryPath).exists()) {
                new File(directoryPath).mkdirs();
            }

            String fileName = new CreateNewKey().createId()+".pdf";
            filePath = directoryPath+fileName;
            File file = new File(filePath);
            if(!file.exists())file.createNewFile();
            fos = new FileOutputStream(filePath);
            while ((size = bis.read(buf)) != -1){
                fos.write(buf, 0, size);
            }
            //关闭流
            fos.close();
            bis.close();
            is.close();
            httpUrl.disconnect();

            //读取pdf
        /*PDFParser parser = new PDFParser(new FileInputStream(new File(fileName)));
        parser.parse();
        PDDocument document = parser.getPDDocument();
        PDFTextStripper stripper = new PDFTextStripper();
        sb.append(stripper.getText(document));*/

            PdfReader reader = new PdfReader(filePath);
            PdfReaderContentParser parser = new PdfReaderContentParser(reader);
            StringBuffer buff = new StringBuffer();
            TextExtractionStrategy strategy;
            for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                strategy = parser.processContent(i,
                        new SimpleTextExtractionStrategy());
                sb.append(strategy.getResultantText());
            }

            //替换特殊字符
            content = sb.toString();
            content = content.replaceAll("\b","").replaceAll("\\a","");
            map.put("content",content);

            //上传至fastdfs服务器
            String serverFilePath = FastDfsUtil.Upload(filePath);
            map.put("serverFilePath",serverFilePath);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //删除文件
            if(!filePath.equals("")){
                File f = new File(filePath);
                if (f.exists()) {
                    f.delete();
                }
            }
        }

        return map;
    }

    public static String getImage(String destUrl) throws IOException {
        StringBuffer sb = new StringBuffer();
        FileOutputStream fos = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        URL url = null;
        byte[] buf = new byte[1024];
        int size = 0;
        String filePath = "";
        String serverFilePath = "";

        try{
            url = new URL(destUrl);
            httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.connect();

            InputStream is = httpUrl.getInputStream();

            //写文件
            bis = new BufferedInputStream(is);

            // 如果不存在就创建文件夹
            String directoryPath = "D:\\test\\";
            directoryPath = "/home/myapp/webCollector/temp";
            if (!new File(directoryPath).exists()) {
                new File(directoryPath).mkdirs();
            }

            String fileName = new CreateNewKey().createId()+destUrl.substring(destUrl.lastIndexOf("."),destUrl.length());
            filePath = directoryPath+fileName;
            File file = new File(filePath);
            if(!file.exists())file.createNewFile();
            fos = new FileOutputStream(filePath);
            while ((size = bis.read(buf)) != -1){
                fos.write(buf, 0, size);
            }

            //关闭流
            fos.close();
            bis.close();
            is.close();
            httpUrl.disconnect();

            //上传至fastdfs服务器
            serverFilePath = FastDfsUtil.Upload(filePath);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //删除文件
            if(!filePath.equals("")){
                File f = new File(filePath);
                if (f.exists()) {
                    f.delete();
                }
            }
        }

        return serverFilePath;
    }
}
